var express = require("express");
var router = express.Router();
const Result = require("../utils/result");
const goodsService = require("../services/goodsService");

//获取商品列表
router.get("/list", async function (req, res) {
  const { page, size, type } = req.query;
  const { rows, count } = await goodsService.list(page, size);
  if (type == 1) {
    const heatGoods = rows.slice(0, 3);
    res.json(Result.success({ heatGoods, goodsArr: rows, count }));
  } else {
    res.json(
      Result.success({
        goodsArr: rows,
        count,
      })
    );
  }
});

//获取热门商品
router.get('/heatGoods',async function (req, res) {
    const heatGoods = await goodsService.heatGoods();
    res.json(Result.success({heatGoods}));
});


//获取商品详情
router.get('/detail',async function (req, res) {
    const {userId,id} =  req.query;
    const goods = await goodsService.getDetail(userId,id);
    res.json(Result.success({goods}));
})

module.exports = router;

